package TeamService;

import Teamdomain.Architect;
import Teamdomain.Designer;
import Teamdomain.Employee;
import Teamdomain.Progammer;


/**
 * @program: Demo2
 * @description:
 * @author: yan guang
 * @create: 2020-04-22 13:15
 */
public class TeamService {
    private static int counter = 1;
    private final int MAX_MEMBER = 5;
    private Progammer[] team = new Progammer[MAX_MEMBER];
    private int total = 0;

    public Progammer[] getTeam() {
        Progammer[] newTeam = new Progammer[total];
        for (int i = 0; i < team.length; i++) {
            newTeam[i] = team[i];
        }
        return newTeam;
    }

    public void addMember(Employee e) throws TeamException {
//        成员已满，无法添加
        if (total >= MAX_MEMBER) {
            throw new TeamException("成员已满，无法添加");
        }
//        该成员不是开发人员，无法添加
        if (!(e instanceof Progammer)) {
            throw new TeamException("该成员不是开发人员，无法添加");
        }
//        该员工已在本开发团队中
        if (isExit(e)) {
            throw new TeamException("该员工已在本开发团队中");
        }
//        该员工已是某团队成员
        Progammer p = (Progammer) e;
        if ("BUSY".equals(p.getStatus().getNAME())) {
            throw new TeamException("该员工已在本开发团队中");
        }
        if ("VOCATION".equals(p.getStatus().getNAME())) {
            throw new TeamException("该员正在休假，无法添加");
        }
//        该员正在休假，无法添加
        int a = 0, b = 0, c = 0;
        for (int i = 0; i < total; i++) {
            if (team[i] instanceof Progammer) {
                a++;
            } else if (team[i] instanceof Designer) {
                b++;
            } else if (team[i] instanceof Architect) {
                c++;
            }
        }
//        团队中至多只能有一名架构师
        if (p instanceof Architect) {
            if (c > 1) {
                throw new TeamException("团队中至多只能有一名架构师，无法添加");
            }
        }
//        团队中至多只能有两名设计师
        if (p instanceof Designer) {
            if (a> 2) {
                throw new TeamException("团队中至多只能有两名设计师，无法添加");
            }
        }
//        团队中至多只能有三名程序员
        if (p instanceof Progammer) {
            if (b > 3) {
                throw new TeamException("团队中至多只能有三名程序员，无法添加");
            }
        }
        p.setStatus(new Status("BUSY"));
        p.setEmberId(counter++);
        team[total++] = p;
    }


    private boolean isExit(Employee e) {
        for (int i = 0; i < team.length; i++) {
            if (team[i].getId() == e.getId()) {
                return true;
            }
        }
        return false;
    }
    public void removeMember(int memberId) throws TeamException {
        int i=0;
        for ( ; i < team.length; i++) {
            if (team[i].getEmberId()==memberId){
                team[i].setStatus(new Status("FREE"));//假如我们想要将一个变量或者一个类从头到尾都是一个的话，可以先声明
                break;
            }
        }
        if(i==total){
            throw new TeamException("没有找到，无法删除");
        }
        for (int j = i; j <total-1 ; j++) {
            team[j]=team[i+1];
        }
        team[--total]=null;
    }
}

